package com.jxs.android.sxyw.db;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.jxs.android.sxyw.boot.SelfApplication;
import com.jxs.android.sxyw.data.BusinessInfo;

/**
 * @Des 业务数据类型
 * @Author jiangxs
 * @Des 2012-8-31 上午9:16:50
 */
public class BusinessHolder {

	static public interface Table {
		public static final String TABLE_NAME = "business_param_tab";
		public static final String PARAM_KEY = "param_key";
		public static final String PARAM_VALUE = "param_value";
		public static final String[] COLUMNS = new String[] { PARAM_KEY, PARAM_VALUE };
	}

	public static void creater(SQLiteDatabase db) {
		StringBuffer createSQL = new StringBuffer(" CREATE TABLE IF NOT EXISTS ");
		createSQL.append(Table.TABLE_NAME);
		createSQL.append(" (_id INTEGER PRIMARY KEY AUTOINCREMENT, ");
		createSQL.append(Table.PARAM_KEY + " varchar(20), ");
		createSQL.append(Table.PARAM_VALUE + " varchar(20) ");
		createSQL.append(" )");
		db.execSQL(createSQL.toString());
	}

	public static void saveOrUpdate(String key, String value) {
		ContentResolver contentResolver = SelfApplication.getApplication().getContentResolver();
		Cursor cursor = null;
		cursor = contentResolver.query(BusinessProvider.CONTENT_URI, Table.COLUMNS, Table.PARAM_KEY + "=? ", new String[] { key }, null);
		ContentValues values = new ContentValues();
		values.put(Table.PARAM_KEY, key);
		values.put(Table.PARAM_VALUE, value);

		if (null != cursor && cursor.getCount() == 0) {
			contentResolver.insert(BusinessProvider.CONTENT_URI, values);
		} else {
			contentResolver.update(BusinessProvider.CONTENT_URI, values, Table.PARAM_KEY + "=? ", new String[] { key });
		}
		if (null != cursor) {
			cursor.close();
		}
	}

	public static String getParamValue(String key) {
		String value = null;
		ContentResolver contentResolver = SelfApplication.getApplication().getContentResolver();
		Cursor cursor = null;
		cursor = contentResolver.query(BusinessProvider.CONTENT_URI, Table.COLUMNS, Table.PARAM_KEY + "=? ", new String[] { key }, null);
		if (null != cursor && cursor.moveToFirst()) {
			value = cursor.getString(cursor.getColumnIndex(Table.PARAM_VALUE));
		}
		if (null != cursor) {
			cursor.close();
		}
		return value;
	}

	public static int counter() {
		int counter = 0;
		Cursor cursor = SelfApplication.getApplication().getContentResolver().query(BusinessProvider.CONTENT_URI, Table.COLUMNS, null, null, null);
		if (null != cursor) {
			counter = cursor.getCount();
			cursor.close();
		}
		return counter;
	}

	public static List<BusinessInfo> queryAll() {
		List<BusinessInfo> results = new ArrayList<BusinessInfo>();
		Cursor cursor = SelfApplication.getApplication().getContentResolver().query(BusinessProvider.CONTENT_URI, Table.COLUMNS, null, null, null);
		if (null != cursor && cursor.moveToFirst()) {
			do {
				BusinessInfo info = new BusinessInfo(cursor.getString(cursor.getColumnIndex(Table.PARAM_KEY)), cursor.getString(cursor.getColumnIndex(Table.PARAM_VALUE)));
				results.add(info);
			} while (cursor.moveToNext());
		}
		if (null != cursor) {
			cursor.close();
		}
		return results;
	}
}
